package cn.shengongying.admin.controller;

import cn.shengongying.admin.dto.UserInDTO;
import cn.shengongying.bean.DataTablePageResult;
import cn.shengongying.bean.ResponseResult;
import cn.shengongying.model.Customer;
import cn.shengongying.model.Information;
import cn.shengongying.model.WorkerList;
import cn.shengongying.service.ICustomerService;
import cn.shengongying.service.IInformationService;
import cn.shengongying.service.IWorkerListService;
import cn.shengongying.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * author: RXK
 * date: 2022/2/9 星期三
 * desc:
 */
@Controller
@RequestMapping("/sys/user")
public class MemberController extends BaseController {


    private final ICustomerService customerService;

    private final IWorkerListService workerListService;

    private final IInformationService informationService;

    public MemberController(ICustomerService customerService, IWorkerListService workerListService, IInformationService informationService) {
        this.customerService = customerService;
        this.workerListService = workerListService;
        this.informationService = informationService;
    }


    @RequestMapping("/index")
    public String index(Model model,Integer type) {
        List<Integer> statistic = customerService.statistic(type);
        model.addAttribute("statistic", statistic);
        return "/user/list";
    }


    @RequestMapping("/boss/index")
    public String bossIndex(Model model,Integer type) {
        List<Integer> statistic = customerService.statistic(type);
        model.addAttribute("statistic", statistic);
        return "/boss/index";
    }

    @ResponseBody
    @RequestMapping("/list")
    public DataTablePageResult<List<Customer>> list(@RequestBody UserInDTO inDTO) {
        List<Customer> list = customerService.listAndPage(inDTO.getNickName(), inDTO.getTelephone(),inDTO.getType(), inDTO.getStart(), inDTO.getLength());
        inDTO.setData(list);
        int count = customerService.calculateCount(inDTO.getNickName(), inDTO.getTelephone(),inDTO.getType());
        inDTO.setRecordsTotal(count);
        return inDTO;
    }

    @ResponseBody
    @RequestMapping("/update/status")
    public ResponseResult<String> updateStatus(@RequestBody UserInDTO inDTO) {
        Customer customer = new Customer();
        customer.setId(inDTO.getId());
        customer.setStatus(inDTO.getStatus());
        customerService.updateById(customer);
        return ResponseResult.success();
    }

    @ResponseBody
    @RequestMapping("/official/msg")
    public ResponseResult<String> sendOfficialMsg(@RequestBody UserInDTO inDTO) {
        if (null == inDTO.getId()) {
            List<Long> userIds = customerService.list(new LambdaQueryWrapper<Customer>().eq(Customer::getType,0))
                    .stream()
                    .map(Customer::getId)
                    .collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(userIds)) {
                List<Information> informationList = userIds.stream()
                        .map(userId -> userMsg(userId, inDTO.getMsg()))
                        .collect(Collectors.toList());
                informationService.save(informationList);
            }
        } else {
            Information information = userMsg(inDTO.getId(), inDTO.getMsg());
            informationService.insert(information);
        }

        return ResponseResult.success();
    }

    private Information userMsg(Long userId, String msg) {
        Information information = new Information();
        information.setUserId(userId);
        information.setType(5);
        information.setContent(msg);
        information.setRead(0);
        information.setFrom(0L);
        return information;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public ResponseResult<String> delete(@RequestBody UserInDTO inDTO) {
        customerService.removeById(inDTO.getId());
        return ResponseResult.success();
    }

    @ResponseBody
    @RequestMapping("/update/disable/time")
    public ResponseResult<String> disableDay(@RequestBody UserInDTO inDTO) {
        Customer customer = new Customer();
        customer.setId(inDTO.getId());
        Date date = DateUtils.addDate(inDTO.getDay());
        customer.setDisableDay(date);
        customerService.updateById(customer);
        return ResponseResult.success();
    }


    @RequestMapping("/worker/list/index")
    public ModelAndView workerList(@RequestParam(value = "userId", required = true) Long userId, ModelAndView modelAndView) {
        modelAndView.addObject("userId", userId);
        modelAndView.setViewName("/user/worker_list");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/workerList")
    public DataTablePageResult<List<WorkerList>> workerList(@RequestBody UserInDTO inDTO) {
        List<WorkerList> workerLists = workerListService.findByUserId(inDTO.getId());
        inDTO.setData(workerLists);
        inDTO.setRecordsTotal(10L);
        return inDTO;
    }


    @ResponseBody
    @RequestMapping("/worker/list/update/status")
    public ResponseResult<String> workerStatusUpdate(@RequestBody UserInDTO inDTO) {
        WorkerList workerList = new WorkerList();
        workerList.setId(inDTO.getId());
        workerList.setStatus(inDTO.getStatus());
        workerListService.updateById(workerList);
        return ResponseResult.success();
    }


}
